-
Notifications
You must be signed in to change notification settings - Fork 2
feat(strings, trie): is prefix of word #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This adds a function `is_prefix_of_word` that enables searching for the index of a `search_word` in a given sentence and return the index if it exists. The index is 1-based. This modifies the `TrieNode` allowing callers to use the `TrieNode` to build a trie from the node and perform prefix searches a well as modifies the `Trie` class as well adding modifications and optimizations that match the changes in the `TrieNode` class. Also note that this change includes an `index` to the `TrieNode` to track the earliest index of a given character in a sentence. That is, what is the earliest that a word has been seen, This allows returning multiple words in the case of using the `Trie` `search` functionality.
|
Caution Review failedThe pull request is closed. WalkthroughAdds a new "Is Prefix" feature under pystrings with implementation, tests, and README; enhances the Trie and TrieNode to track and return word indices; updates a README image path, adds a happy number test, and a minor comment wording fix. Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant API as is_prefix_of_word
participant Trie as Trie/TrieNode
Caller->>API: is_prefix_of_word(sentence, search_word)
API->>Trie: create root TrieNode
loop insert each word with 1-based index
API->>Trie: insert(word, index)
Trie->>Trie: traverse chars, set node.index = min(node.index, index)
Trie->>Trie: mark end_of_word
end
API->>Trie: search_prefix(search_word)
alt path exists for all chars
Trie-->>API: return node.index (or -1 if unset)
else missing char
Trie-->>API: return -1
end
API-->>Caller: return index or -1
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Nitpick comments (1)
pystrings/is_prefix/test_is_prefix_of_word.py (1)
5-54: LGTM! Comprehensive test coverage.The test suite covers the key scenarios:
- Prefix matching at different positions (beginning, middle, end)
- No match cases returning -1
- Multiple words sharing a prefix (verifies earliest index is returned)
- Proper 1-based indexing
Consider adding edge case tests for completeness (though current coverage is good):
- Empty sentence or search_word
- search_word equals an entire word (not just prefix)
- Single-word sentence
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (18)
algorithms/fast_and_slow/happy_number/images/example/example_1_1.pngis excluded by!**/*.pngalgorithms/fast_and_slow/happy_number/images/example/example_1_2.pngis excluded by!**/*.pngalgorithms/fast_and_slow/happy_number/images/example/example_1_3.pngis excluded by!**/*.pngalgorithms/fast_and_slow/happy_number/images/example/example_2_1.pngis excluded by!**/*.pngalgorithms/fast_and_slow/happy_number/images/example/example_2_2.pngis excluded by!**/*.pngalgorithms/fast_and_slow/happy_number/images/example/example_2_3.pngis excluded by!**/*.pngalgorithms/fast_and_slow/happy_number/images/solution/solution_example_1.pngis excluded by!**/*.pngalgorithms/fast_and_slow/happy_number/images/solution/solution_example_2.pngis excluded by!**/*.pngalgorithms/fast_and_slow/happy_number/images/solution/solution_example_3.pngis excluded by!**/*.pngalgorithms/fast_and_slow/happy_number/images/solution/solution_example_4.pngis excluded by!**/*.pngalgorithms/fast_and_slow/happy_number/images/solution/solution_example_5.pngis excluded by!**/*.pngalgorithms/fast_and_slow/happy_number/images/solution/solution_example_6.pngis excluded by!**/*.pngalgorithms/fast_and_slow/happy_number/images/solution/solution_example_7.pngis excluded by!**/*.pngalgorithms/fast_and_slow/happy_number/images/solution/solution_example_8.pngis excluded by!**/*.pngpystrings/is_prefix/images/examples/is_prefix_example_1.pngis excluded by!**/*.pngpystrings/is_prefix/images/examples/is_prefix_example_2.pngis excluded by!**/*.pngpystrings/is_prefix/images/examples/is_prefix_example_3.pngis excluded by!**/*.pngpystrings/is_prefix/images/examples/is_prefix_example_4.pngis excluded by!**/*.png
📒 Files selected for processing (9)
DIRECTORY.md(1 hunks)algorithms/fast_and_slow/happy_number/README.md(1 hunks)algorithms/fast_and_slow/happy_number/test_happy_number.py(1 hunks)datastructures/streams/stream_checker/__init__.py(1 hunks)datastructures/trees/trie/trie.py(3 hunks)datastructures/trees/trie/trie_node.py(2 hunks)pystrings/is_prefix/README.md(1 hunks)pystrings/is_prefix/__init__.py(1 hunks)pystrings/is_prefix/test_is_prefix_of_word.py(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (5)
pystrings/is_prefix/__init__.py (2)
datastructures/trees/trie/trie_node.py (3)
TrieNode(5-62)insert(26-41)search_prefix(43-62)datastructures/trees/trie/trie.py (1)
insert(9-49)
pystrings/is_prefix/test_is_prefix_of_word.py (1)
pystrings/is_prefix/__init__.py (1)
is_prefix_of_word(4-23)
datastructures/trees/trie/trie.py (1)
datastructures/trees/trie/trie_node.py (2)
TrieNode(5-62)insert(26-41)
algorithms/fast_and_slow/happy_number/test_happy_number.py (1)
algorithms/fast_and_slow/happy_number/__init__.py (1)
is_happy_number_2(39-102)
datastructures/trees/trie/trie_node.py (1)
datastructures/trees/trie/trie.py (1)
insert(9-49)
🪛 LanguageTool
algorithms/fast_and_slow/happy_number/README.md
[style] ~33-~33: Using many exclamation marks might seem excessive (in this case: 8 exclamation marks for a text that’s 1193 characters long)
Context: ...images/solution/solution_example_1.png)

🪛 markdownlint-cli2 (0.18.1)
DIRECTORY.md
710-710: Unordered list indentation
Expected: 0; Actual: 2
(MD007, ul-indent)
711-711: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
🔇 Additional comments (9)
algorithms/fast_and_slow/happy_number/test_happy_number.py (1)
116-120: Test looks correct but appears unrelated to PR objectives.The test correctly verifies that 20 is not a happy number (it cycles: 20→4→16→37→58→89→145→42→20). However, this change seems unrelated to the PR objectives, which describe adding
is_prefix_of_wordfunctionality and Trie modifications.algorithms/fast_and_slow/happy_number/README.md (1)
18-40: Image path reorganization looks good but appears unrelated to PR objectives.The image paths have been appropriately updated to use an organized directory structure (images/example/ and images/solution/). However, like the test file changes, this documentation update seems unrelated to the PR objectives about adding
is_prefix_of_wordfunctionality and Trie modifications.Note: The static analysis hint about excessive exclamation marks is a false positive—these are Markdown image syntax (
), not punctuation.datastructures/streams/stream_checker/__init__.py (1)
61-61: LGTM! Minor grammar improvement.The hyphenation of "dead-end" as a compound adjective is grammatically correct and improves readability.
DIRECTORY.md (1)
710-711: LGTM! Documentation update for new feature.The new entries correctly document the "Is Prefix" feature and follow the existing structure and indentation pattern used throughout the file.
pystrings/is_prefix/README.md (1)
1-24: LGTM! Clear and comprehensive documentation.The README provides a well-structured problem description with constraints and examples. The explanation of prefix definition and return value semantics (1-based indexing, earliest match, -1 for no match) aligns well with the implementation.
pystrings/is_prefix/__init__.py (1)
4-23: LGTM! Clean and correct implementation.The function correctly:
- Builds a Trie from sentence words with 1-based indices (line 19)
- Delegates prefix search to the TrieNode implementation
- Returns the appropriate index or -1
The implementation is straightforward and matches the documented behavior.
datastructures/trees/trie/trie_node.py (2)
1-1: LGTM! Clean addition of index tracking.The new
indexattribute with proper typing and inclusion in__repr__provides good foundation for tracking word positions in the Trie.Also applies to: 21-21, 24-24
26-41: LGTM! Correct index tracking implementation.The
insertmethod properly maintains the minimum index along each character path usingcurr.index or float("inf")to handle the None case. This ensures that when multiple words share a prefix, the earliest word's index is preserved at each node.datastructures/trees/trie/trie.py (1)
88-89: LGTM! Helpful debugging addition.The
__repr__method provides a useful string representation that will recursively show the trie structure via the TrieNode's__repr__method.
…onSnips into feat/is-prefix-of-word rigin feat/is-
Describe your change:
This adds a function
is_prefix_of_wordthat enables searching for theindex of a
search_wordin a given sentence and return the index if itexists. The index is 1-based.
This modifies the
TrieNodeallowing callers to use theTrieNodetobuild a trie from the node and perform prefix searches a well as
modifies the
Trieclass as well adding modifications and optimizationsthat match the changes in the
TrieNodeclass.Also note that this change includes an
indexto theTrieNodetotrack the earliest index of a given character in a sentence. That is,
what is the earliest that a word has been seen, This allows returning
multiple words in the case of using the
Triesearchfunctionality.Checklist:
Fixes: #{$ISSUE_NO}.Summary by CodeRabbit
New Features
Documentation
Tests
Style
✏️ Tip: You can customize this high-level summary in your review settings.